<template>
  <div id="suggestorder">
    <el-button size="mini" type="text" @click="to">回访</el-button>
    <el-dialog :append-to-body="true" :visible.sync="dialog" :title="'回访'" width="680px">
      <el-form ref="form" :model="form" :inline="true" :rules="rules" size="small" label-width="80px">
        <el-form-item label="是否到场" >
          <el-select v-model="form.present" clearable class="filter-item" style="width: 200px;">
            <el-option v-for="item in presentOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
          </el-select>
        </el-form-item>
        <el-form-item label="到场时间" >
          <el-date-picker
            v-model="form.presentTime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间" style="width: 200px;">
        </el-date-picker>
        </el-form-item>
        <el-form-item label="满意度" v-show="data.status === 500">
          <el-select v-model="form.satisfaction" clearable class="filter-item" style="width: 200px;">
            <el-option v-for="item in satisfactionOptions" :key="item.key" :label="item.display_name" :value="item.key"/>
          </el-select>
        </el-form-item>
        <el-form-item label="服务时长" v-show="data.status === 500">
          <el-input v-model="form.serviceTime" style="width: 200px;"/>
        </el-form-item>
        <el-form-item label="回访内容" v-show="data.status === 500">
          <el-input v-model="form.content" style="width: 495px;" rows="5" type="textarea"/>
        </el-form-item>
        <el-form-item label="意见建议" v-show="data.status === 500">
          <el-input v-model="form.suggest" style="width: 495px;" rows="5" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
        <el-button type="text" @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { add, edit } from '@/api/tCustomerSuggest'
import request from '@/utils/request'
import { parseTime } from '@/utils/index'
export default {
  name: "suggestOrder",
  props: {
    data: {
      type: Object,
      required: true
    },
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      presentOptions: [
        { key: '1', display_name: '是' },
        { key: '0', display_name: '否' }
      ],
      satisfactionOptions:[
        { key: '0', display_name: '不满意' },
        { key: '1', display_name: '基本满意'},
        { key: '2', display_name: '满意' },
      ],
      loading: false, dialog: false,
      form: {
        id: '',
        orderCode: '',
        present: '',
        presentTime: '',
        satisfaction: '',
        serviceTime: '',
        requireTool: '',
        content: '',
        suggest: '',
        inputOperator: '',
        inputTime: ''
      },
      rules: {
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    init() {
      const params = {
        orderCode:this.data.orderCode
      }
      request({
        url: 'api/tCustomerSuggest',
        method: 'get',
        params
      }).then(res => {
        const content = [].concat(res.content);
        const _this = this
        content.forEach(function(data, index) {
          if(data!=null){
            _this.form.id=data.id;
            _this.form.orderCode=data.orderCode;
            _this.form.present=data.present==1?'是':'否';
            _this.form.presentTime=new Date(data.presentTime);
            _this.form.satisfaction=data.satisfaction;
            _this.form.serviceTime=data.serviceTime;
            _this.form.requireTool=data.requireTool;
            _this.form.content=data.content;
            _this.form.suggest=data.suggest;
            return;
          }
        })
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    to() {
      this.form.orderCode = this.data.orderCode;
      this.init();
      this.dialog = true
    },
    doSubmit() {
      this.loading = true
      if(this.form.present=='是'){
        this.form.present=1
      } else {
        this.form.present=0
      }
      if (this.form.id=='' || this.form.id==null) {
        this.doAdd()
      } else{
        this.form.presentTime = parseTime(this.form.presentTime)
        this.doEdit()
      } 
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        orderCode: '',
        present: '',
        presentTime: '',
        satisfaction: '',
        serviceTime: '',
        requireTool: '',
        content: '',
        suggest: '',
        inputOperator: '',
        inputTime: ''
      }
    }
  }
}
</script>

<style scoped>
 #suggestorder{
    display: inline-block;
    margin-right: 3px;
  }
</style>